package com.example.xinhuazidian.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.xinhuazidian.R
import com.example.xinhuazidian.bean.Idiom

class IdiomAdapter(private val list: MutableList<Idiom>) : RecyclerView.Adapter<IdiomAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val word: TextView = view.findViewById(R.id.tv_word)
        val pinyin: TextView = view.findViewById(R.id.tv_pinyin)
        val abbreviation: TextView = view.findViewById(R.id.tv_abbreviation)
        val explanation: TextView = view.findViewById(R.id.tv_explanation)
        val derivation: TextView = view.findViewById(R.id.tv_derivation)
        val ll: LinearLayout = view.findViewById(R.id.ll)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_idiom, parent, false)
        return MyViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val appInfo = this.list[position]
        with(holder) {
            word.text = appInfo.word
            pinyin.text = appInfo.pinyin
            abbreviation.text = appInfo.abbreviation
            explanation.text = "解释：${appInfo.explanation}"
            derivation.text = "出自：${appInfo.derivation}"

            if (appInfo.zhanKai == false) {
                derivation.visibility = View.GONE
            } else {
                derivation.visibility = View.VISIBLE
            }

            ll.setOnClickListener {
                if (appInfo.zhanKai == false) {
                    derivation.visibility = View.VISIBLE
                    appInfo.zhanKai = true
                } else {
                    derivation.visibility = View.GONE
                    appInfo.zhanKai = false
                }
            }
        }
    }

    override fun getItemCount() = list.size
}

